package com.boge.flowabley;

import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
class FlowableyTaskListinerTestsFunction {
    @Autowired
    private ProcessEngine processEngine;

    @Autowired
    private RuntimeService runtimeService;

    //任务待办的注解
    @Autowired
    private TaskService taskService;


    /**
     * 部署一个流程：任务监听器：com.boge.flowabley.listener.MyListiner
     */
    @Test
    void deployProcess() {

        Deployment deploy = processEngine.getRepositoryService().createDeployment()
                .addClasspathResource("process/ListinerFlow.bpmn20.xml") // 部署一个流程
                .name("任务监听器案例")
                .deploy();
        System.out.println(deploy.getId()); //a5f00df8-c5e5-11ef-9c70-de708c98c2b1
    }

    /**
     * 发起一个流程
     */
    @Test
    void startProcess() {
        String processId="ListinerFlowKey:6:e74cef57-c5eb-11ef-91fa-fe4995b30266"; //来源于表 ACT_RE_PROCDEF 的 ID_
        String processKey="ListinerFlowKey"; //来源于表 ACT_RE_PROCDEF 的 KEY_

        //${myBean.getAssignee()}
        Map<String, Object> variables=new HashMap<>();
        variables.put("processId", processId);
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processId);

        //MyListiner ------------ notify ------执行了 ------


    }

    /**
     * 根据用户查询待办
     */
    @Test
    void findFlowByUser() {
        List<Task> list=  taskService.createTaskQuery().taskAssignee("lisi ").list();
        for(Task task:list){
            System.out.println(task.getId());
        }

    }

    /**
     * 完成任务的审批
     */
    @Test
    void completeTask() {
        //根据id 完成任务审批
        taskService.complete("8d27707e-c58c-11ef-981b-2a8e37b4bb40");

    }

    /**
     * 挂起流程
     * 激活流程
     */
    @Test
    void suspendActivity(){
        String prcessDefinitionId="FirstFlow:9:3459536f-c4c7-11ef-bc58-fef18153b082";
        RepositoryService repositoryService = processEngine.getRepositoryService();
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionId(prcessDefinitionId)
                .singleResult();
        // 获取流程定义的状态
        boolean suspended = processDefinition.isSuspended();
        System.out.println("suspended = " + suspended);
        if(suspended){
            // 表示被挂起,需要被激活
            System.out.println("当前是挂起状态，开始激活流程定义");
            repositoryService.activateProcessDefinitionById(prcessDefinitionId,true,null);
        }else{
            // 表示激活状态
            System.out.println("当前是激活状态，开始挂起流程");
            repositoryService.suspendProcessDefinitionById(prcessDefinitionId,true,null);
        }
    }


    /**
     * 挂起流程实例
     */
    @Test
    void suspendInstance(){
        // 挂起流程实例
        runtimeService.suspendProcessInstanceById("91eb3c1b-c521-11ef-a085-1a99d71cb7b3");
        // 激活流程实例
        //runtimeService.activateProcessInstanceById("91eb3c1b-c521-11ef-a085-1a99d71cb7b3");
    }


}
